<!--
 * @Descripttion: 自带插件组件
 * @Author: caijj
 * @Date: 2020-12-25 10:49:44
 * @LastEditors: caijj
 * @LastEditTime: 2020-12-25 10:50:03
-->
<template>
  <div :class="{'hidden':hidden}" class="pagination-container" :style="{textAlign: textAlign}">
    <el-pagination
      :background="background"
      :current-page.sync="currentPage"
      :page-size.sync="size"
      :layout="layout"
      :page-sizes="pageSizes"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
      <slot />
    </el-pagination>
  </div>
</template>

<script>
import { scrollTo } from '@/utils/scroll-to';

export default {
  name: 'Pagination',
  props: {
    textAlign: {
      type: String,
      default: 'left'
    },
    total: {
      required: true,
      type: Number
    },
    page: {
      type: Number,
      default: 0
    },
    limit: {
      type: Number,
      default: 20
    },
    pageSizes: {
      type: Array,
      default() {
        return [10, 20, 30, 50];
      }
    },
    layout: {
      type: String,
      default: 'total, slot, sizes, prev, pager, next, jumper'
    },
    background: {
      type: Boolean,
      default: true
    },
    autoScroll: {
      type: Boolean,
      default: true
    },
    hidden: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    currentPage: {
      get() {
        return this.page;
      },
      set(val) {
        this.$emit('update:page', val);
      }
    },
    size: {
      get() {
        return this.limit;
      },
      set(val) {
        this.$emit('update:limit', val);
      }
    }
  },
  methods: {
    handleSizeChange(val) {
      this.$emit('pagination', { page: this.currentPage, limit: val });
      if (this.autoScroll) {
        scrollTo(0, 800);
      }
    },
    handleCurrentChange(val) {
      this.$emit('pagination', { page: val, limit: this.size });
      if (this.autoScroll) {
        scrollTo(0, 800);
      }
    }
  }
};
</script>

<style lang="scss">
.pagination-container {
  background: #fff;
  padding: 32px 16px;
}
.pagination-container.hidden {
  display: none;
}
// 重置分页样式
#app {
  .el-pagination.is-background {
    button {
      background: transparent;
      border: 1px solid rgba(217, 217, 217, 1);
    }
    .el-pager li {
      background: transparent;
      border: 1px solid rgba(217, 217, 217, 1);
      font-weight: normal;
      border-radius: 4px;
      &.active {
        background: rgba(24, 144, 255, 1);
      }
    }
  }
}
</style>
